package com.jzg.jcptadmin.presenter;/**
 * author: gcc
 * date: 2016/7/7 15:02
 * email:
 */

import com.jzg.jcptadmin.base.BasePresenter;
import com.jzg.jcptadmin.data.DataManager;
import com.jzg.jcptadmin.data.vo.CityItem;
import com.jzg.jcptadmin.data.vo.OrderStatisticsData;
import com.jzg.jcptadmin.viewinterface.OrderInterface;

import java.util.Map;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * author: guochen
 * date: 2016/7/7 15:02
 * email: 
 */
public class OrderPresenter extends BasePresenter<OrderInterface> {
    private DataManager dataManager;
    private OrderInterface mView;
    private Subscription mSubscription;

    public OrderPresenter(DataManager dataManager){
        this.dataManager = dataManager;
    }


    @Override
    public void attachView(OrderInterface mvpView) {
        super.attachView(mvpView);
        this.mView = mvpView;
    }
    @Override
    public void detachView() {
        super.detachView();
        if (mSubscription != null) mSubscription.unsubscribe();
    }

    public void getCityData(Map<String,String> params) {
        mSubscription = dataManager.getCityData(params)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.newThread()).subscribe(new Observer<CityItem>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                if(e!=null&& mView!=null){
                    mView.showError(e.getMessage());
                }
            }

            @Override
            public void onNext(CityItem cityItem) {

                mView.cityDataSuccess(cityItem);


            }
        });
    }
    public void getOrderData(Map<String,String> params) {
        mSubscription = dataManager.getOrderData(params)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.newThread()).subscribe(new Observer<OrderStatisticsData>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                if(e!=null&& mView!=null){
                    mView.showError(e.getMessage());
                }
            }

            @Override
            public void onNext(OrderStatisticsData orderStatisticsData) {

                mView.OrderDataSuccess(orderStatisticsData);


            }
        });
    }
}
